#include "message.h"
#include "ui_message.h"
#include "QMenu"
#include "QMessageBox"
#include <QDebug>
#include <main.h>
extern QMap<QString, user*> userMap;

message::message(user* user,QString nhistory, QString ntime,  QWidget *parent) :
    QWidget(parent),
    user_id(user->id),
    ui(new Ui::message)
{
    ui->setupUi(this);

    set_history(nhistory);
    set_name(user->name);
    set_picture(user->picture);
    set_time(ntime);
    set_readnum(false);

    ui->widget_page->installEventFilter(this);    // 安装事件过滤器
    setMouseTracking(true); //鼠标追踪
}

message::~message()
{
    delete ui;
}


void message::set_picture(QPixmap picture)
{
    picture = picture.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    int width = ui->label->size().width();
    int height = ui->label->size().height();
    QPixmap image(width,height);
    image.fill(Qt::transparent);

    QPainterPath painterPath;
    painterPath.addEllipse(0, 0, width, height);

    QPainter painter(&image);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setClipPath(painterPath);
    painter.drawPixmap(0, 0, width, height, picture);
    ui->label->setPixmap(image);
}

void message::set_name(QString name)
{
    ui->label_name->setText(name);
}

//设置历史信息
void message::set_history(QString history)
{
    ui->label_message->setText(history);
}

//最后一条信息的发送时间显示
void message::set_time(QString time)
{
    ui->label_time->setText(time);
}

void message::set_readnum(bool flag){
    if(flag){
        num++;
        ui->label_num->setText(QString::number(num));
    }
    else{
        ui->label_num->setText(" ");
        num = 0;
    }
}
bool message::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->widget_page){
        //悬浮，变深
        if (event->type() == QEvent::Enter) {
            // 当鼠标进入时，将背景颜色变深
            qDebug() <<"进入";
            QPalette palette = ui->widget_page->palette();
            palette.setColor(QPalette::Window, QColor(200,200,200));
            ui->widget_page->setAutoFillBackground(true);
            ui->widget_page->setPalette(palette);
            ui->widget_page->update(); // 更新以显示新的背景颜色
            return true;
        }
        //悬浮，恢复
        else if (event->type() == QEvent::Leave) {
            // 当鼠标离开时，恢复原来的背景颜色
            qDebug() <<"离开";
            QPalette palette = ui->widget_page->palette();
            palette.setColor(QPalette::Window, Qt::transparent);
            ui->widget_page->setAutoFillBackground(true);
            ui->widget_page->setPalette(palette);
            ui->widget_page->update(); // 更新以显示新的背景颜色
            return true;
        }
        //单击
        else if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            // 在左键点击时，打开聊天框
            if (mouseEvent->button() == Qt::LeftButton) {
                qDebug()<<"左键选择";
                set_readnum(false);//设置未读消息已读
                //私聊界面
                if (userMap.contains(user_id)){
                    userMap.value(user_id)->show_chat();
                }
//                //打开群聊的界面
                if (groupMap.contains(user_id)){
                    groupMap.value(user_id)->show_chat();
                }
            }
            // 在右键点击时弹出选择窗口
            else if (mouseEvent->button() == Qt::RightButton) {
                qDebug()<<"右键选择";
                QPoint pos = mapToGlobal(mouseEvent->pos());
                QMenu contextMenu;
                QAction action_one("详细信息", this);
                connect(&action_one, &QAction::triggered, [this]() {
                    // 处理选择窗口的操作
                    if (userMap.contains(user_id)){
                        userMap.value(user_id)->show_info();
                    }
//                    QMessageBox::information(this, "窗口", "您选择了详细信息");
                });
                contextMenu.addAction(&action_one);
                contextMenu.exec(pos);
                return true;
            }
        }
        else{
            return false;
        }

    }
    return QWidget::eventFilter(obj, event);
}
